package com.fishingwithme.domain.spots;

import com.fishingwithme.infrastructure.CodeEnum;
import com.fishingwithme.infrastructure.utils.CommonUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Getter;
import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum WaterType implements CodeEnum {
    OCEAN(1, "海洋"),
    RIVER(2, "河流"),
    LAKE(3, "湖泊"),
    RESERVOIR(4, "水库"),
    POND(5, "野塘"),
    STREAM(6, "溪流"),
    HEI_KENG(7, "黑坑"),
    OTHER(8, "其他");

    @JsonProperty("type")
    private int type;
    @JsonProperty("name")
    private String name;

    WaterType(int type, String name) {
        this.type = type;
        this.name = name;
    }

    // 重写 toString() 方法，返回 JSON 格式字符串
    @Override
    public String toString() {
        try {
            return toJson().toString();
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    // 返回 JSON 对象
    public ObjectNode toJson() throws JSONException {
        ObjectNode json = CommonUtils.objectMapper.createObjectNode();
        json.put("code", this.name());
        json.put("type", this.type);
        json.put("name", this.name);
        return json;
    }

    // 根据 type 查找枚举
    private static final Map<Integer, WaterType> TYPE_MAP = new HashMap<>();

    static {
        for (WaterType waterType : values()) {
            TYPE_MAP.put(waterType.type, waterType);
        }
    }

    public static WaterType of(int type) {
        return TYPE_MAP.getOrDefault(type, null);
    }
}
